/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.AccessManager.ViaGrant;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.MailTarget;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.RightCommand;
/*     */ import com.zimbra.cs.account.accesscontrol.RightModifier;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.User;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.HashMap;
/*     */ import java.util.Locale;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestDistListACL
/*     */   extends TestCase
/*     */ {
/*  49 */   final String otherDomain = "other.example.test";
/*     */   String listAddress;
/*     */   String listAddress2;
/*     */   String auser;
/*     */   String alias;
/*     */   String dlalias;
/*     */   Provisioning prov;
/*     */   AccessManager accessMgr;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  61 */     this.listAddress = String.format("testdistlistacl@%s", new Object[] { TestUtil.getDomain() });
/*  62 */     this.listAddress2 = String.format("testDLacl2@%s", new Object[] { TestUtil.getDomain() });
/*  63 */     this.auser = String.format("userWithAlias@%s", new Object[] { TestUtil.getDomain() });
/*  64 */     this.alias = String.format("alias@%s", new Object[] { "other.example.test" });
/*  65 */     this.dlalias = String.format("dlalias@%s", new Object[] { "other.example.test" });
/*  66 */     this.prov = Provisioning.getInstance();
/*  67 */     this.accessMgr = AccessManager.getInstance();
/*  68 */     tearDown();
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/*  73 */     DistributionList dl = this.prov.get(Key.DistributionListBy.name, this.listAddress);
/*  74 */     if (dl != null) {
/*  75 */       this.prov.deleteDistributionList(dl.getId());
/*     */     }
/*  77 */     dl = this.prov.get(Key.DistributionListBy.name, this.listAddress2);
/*  78 */     if (dl != null) {
/*  79 */       this.prov.deleteDistributionList(dl.getId());
/*     */     }
/*  81 */     Account acct = this.prov.get(Key.AccountBy.name, this.auser);
/*  82 */     if (acct != null) {
/*  83 */       this.prov.deleteAccount(acct.getId());
/*     */     }
/*  85 */     Domain domain = this.prov.get(Key.DomainBy.name, "other.example.test");
/*  86 */     if (domain != null) {
/*  87 */       this.prov.deleteDomain(domain.getId());
/*     */     }
/*     */   }
/*     */   
/*     */   private void doCheckSentToDistListGuestRight(DistributionList targetDl, String email, String guest, boolean expected)
/*     */     throws ServiceException
/*     */   {
/*  94 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { targetDl.getName(), targetDl.getId() });
/*  95 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/*  96 */     assertNotNull("Unable to find Group object for DL by name", group);
/*  97 */     AccessManager.ViaGrant via = new AccessManager.ViaGrant();
/*  98 */     NamedEntry ne = GranteeType.lookupGrantee(this.prov, GranteeType.GT_GUEST, GranteeSelector.GranteeBy.name, email);
/*  99 */     MailTarget grantee = null;
/* 100 */     if ((ne instanceof MailTarget)) {
/* 101 */       grantee = (MailTarget)ne;
/*     */     }
/* 103 */     boolean result = RightCommand.checkRight(this.prov, "dl", TargetBy.name, this.listAddress, grantee, "sendToDistList", null, via);
/*     */     
/* 105 */     if (expected) {
/* 106 */       assertTrue(String.format("%s should be able to send to DL (as guest %s)", new Object[] { email, guest }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 108 */       assertTrue(String.format("%s should have right to send to DL (as guest %s)", new Object[] { email, guest }), result);
/*     */       
/* 110 */       ZimbraLog.test.info("Test for %s against dom %s Via=%s", new Object[] { email, guest, via });
/*     */     } else {
/* 112 */       assertFalse(String.format("%s should NOT be able to send to DL (because not guest %s)", new Object[] { email, guest }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 114 */       assertFalse(String.format("%s should NOT have right to send to DL (because not guest %s)", new Object[] { email, guest }), result);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterGuestSendToDL()
/*     */     throws Exception
/*     */   {
/* 124 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 125 */     String guestName = "fred@example.test";
/* 126 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_GUEST.getCode(), GranteeSelector.GranteeBy.name, guestName, "", "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 129 */     doCheckSentToDistListGuestRight(dl, guestName, guestName, true);
/* 130 */     doCheckSentToDistListGuestRight(dl, "pete@example.test", guestName, false);
/*     */     
/* 132 */     doCheckSentToDistListGuestRight(dl, "FreD@example.test", guestName, true);
/*     */   }
/*     */   
/*     */   private void doCheckSentToDistListEmailRight(DistributionList targetDl, String email, String grantEmail, boolean expected)
/*     */     throws ServiceException
/*     */   {
/* 138 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { targetDl.getName(), targetDl.getId() });
/* 139 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/* 140 */     assertNotNull("Unable to find Group object for DL by name", group);
/* 141 */     AccessManager.ViaGrant via = new AccessManager.ViaGrant();
/* 142 */     NamedEntry ne = GranteeType.lookupGrantee(this.prov, GranteeType.GT_EMAIL, GranteeSelector.GranteeBy.name, email);
/* 143 */     MailTarget grantee = null;
/* 144 */     if ((ne instanceof MailTarget)) {
/* 145 */       grantee = (MailTarget)ne;
/*     */     }
/* 147 */     boolean result = RightCommand.checkRight(this.prov, "dl", TargetBy.name, this.listAddress, grantee, "sendToDistList", null, via);
/*     */     
/* 149 */     if (expected) {
/* 150 */       assertTrue(String.format("%s should be able to send to DL (using email %s)", new Object[] { email, grantEmail }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 152 */       assertTrue(String.format("%s should have right to send to DL (using email %s)", new Object[] { email, grantEmail }), result);
/*     */       
/* 154 */       ZimbraLog.test.info("Test for %s against dom %s Via=%s", new Object[] { email, grantEmail, via });
/*     */     } else {
/* 156 */       assertFalse(String.format("%s should NOT be able to send to DL (because not email %s)", new Object[] { email, grantEmail }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 158 */       assertFalse(String.format("%s should NOT have right to send to DL (because not email %s)", new Object[] { email, grantEmail }), result);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterEmailSendToDL()
/*     */     throws Exception
/*     */   {
/* 168 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 169 */     String guestName = "fred@example.test";
/* 170 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, guestName, null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 173 */     this.prov.createDistributionList(this.listAddress2, new HashMap());
/* 174 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, this.listAddress2, null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 177 */     String user1email = TestUtil.getAddress("user1");
/* 178 */     String user2email = TestUtil.getAddress("user2");
/* 179 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, user1email, null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 182 */     doCheckSentToDistListEmailRight(dl, guestName, guestName, true);
/* 183 */     doCheckSentToDistListEmailRight(dl, "pete@example.test", guestName, false);
/* 184 */     doCheckSentToDistListEmailRight(dl, "FreD@example.test", guestName, true);
/* 185 */     doCheckSentToDistListEmailRight(dl, this.listAddress2, this.listAddress2, true);
/* 186 */     doCheckSentToDistListEmailRight(dl, this.listAddress, this.listAddress2, false);
/* 187 */     doCheckSentToDistListEmailRight(dl, user1email.toUpperCase(Locale.ENGLISH), user1email, true);
/* 188 */     doCheckSentToDistListEmailRight(dl, user2email, user1email, false);
/* 189 */     this.prov.revokeRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, guestName, "sendToDistList", (RightModifier)null);
/*     */     
/* 191 */     this.prov.revokeRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, this.listAddress2, "sendToDistList", (RightModifier)null);
/*     */     
/* 193 */     this.prov.revokeRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EMAIL.getCode(), GranteeSelector.GranteeBy.name, user1email, "sendToDistList", (RightModifier)null);
/*     */     
/* 195 */     doCheckSentToDistListEmailRight(dl, user2email, "no grants in place", true);
/*     */   }
/*     */   
/*     */   private void doCheckSentToDistListUserRight(DistributionList targetDl, String email, String user, boolean expected)
/*     */     throws ServiceException
/*     */   {
/* 201 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { targetDl.getName(), targetDl.getId() });
/* 202 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/* 203 */     assertNotNull("Unable to find Group object for DL by name", group);
/* 204 */     AccessManager.ViaGrant via = new AccessManager.ViaGrant();
/*     */     
/* 206 */     NamedEntry ne = GranteeType.lookupGrantee(this.prov, GranteeType.GT_EMAIL, GranteeSelector.GranteeBy.name, email);
/* 207 */     MailTarget grantee = null;
/* 208 */     if ((ne instanceof MailTarget)) {
/* 209 */       grantee = (MailTarget)ne;
/*     */     }
/* 211 */     boolean result = RightCommand.checkRight(this.prov, "dl", TargetBy.name, this.listAddress, grantee, "sendToDistList", null, via);
/*     */     
/* 213 */     if (expected) {
/* 214 */       assertTrue(String.format("%s should be able to send to DL (as user %s)", new Object[] { email, user }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 216 */       assertTrue(String.format("%s should have right to send to DL (as user %s)", new Object[] { email, user }), result);
/*     */       
/* 218 */       ZimbraLog.test.info("Test for %s against dom %s Via=%s", new Object[] { email, user, via });
/*     */     } else {
/* 220 */       assertFalse(String.format("%s should NOT be able to send to DL (because not user %s)", new Object[] { email, user }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 222 */       assertFalse(String.format("%s should NOT have right to send to DL (because not user %s)", new Object[] { email, user }), result);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterUserSendToDL()
/*     */     throws Exception
/*     */   {
/* 232 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 233 */     String user1email = TestUtil.getAddress("user1");
/* 234 */     String user2email = TestUtil.getAddress("user2");
/* 235 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, user1email, null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 238 */     doCheckSentToDistListUserRight(dl, user1email, user1email, true);
/* 239 */     doCheckSentToDistListUserRight(dl, "pete@example.test", user1email, false);
/* 240 */     doCheckSentToDistListUserRight(dl, user2email, user1email, false);
/* 241 */     doCheckSentToDistListUserRight(dl, user1email.toUpperCase(Locale.ENGLISH), user1email, true);
/*     */   }
/*     */   
/*     */   private void doCheckSentToDistListDomRight(DistributionList targetDl, String email, String grantDomain, boolean expected) throws ServiceException
/*     */   {
/* 246 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { targetDl.getName(), targetDl.getId() });
/* 247 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/* 248 */     assertNotNull("Unable to find Group object for DL by name", group);
/* 249 */     AccessManager.ViaGrant via = new AccessManager.ViaGrant();
/* 250 */     NamedEntry ne = GranteeType.lookupGrantee(this.prov, GranteeType.GT_EMAIL, GranteeSelector.GranteeBy.name, email);
/* 251 */     MailTarget grantee = null;
/* 252 */     if ((ne instanceof MailTarget)) {
/* 253 */       grantee = (MailTarget)ne;
/*     */     }
/* 255 */     boolean result = RightCommand.checkRight(this.prov, "dl", TargetBy.name, this.listAddress, grantee, "sendToDistList", null, via);
/*     */     
/* 257 */     if (expected) {
/* 258 */       assertTrue(String.format("%s should be able to send to DL (because in domain %s)", new Object[] { email, grantDomain }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 260 */       assertTrue(String.format("%s should have right to send to DL (because in domain %s)", new Object[] { email, grantDomain }), result);
/*     */       
/* 262 */       ZimbraLog.test.info("Test for %s against dom %s Via=%s", new Object[] { email, grantDomain, via });
/*     */     } else {
/* 264 */       assertFalse(String.format("%s should NOT be able to send to DL (because not in domain %s)", new Object[] { email, grantDomain }), this.accessMgr.canDo(email, group, Rights.User.R_sendToDistList, false));
/*     */       
/* 266 */       assertFalse(String.format("%s should NOT have right to send to DL (because not in domain %s)", new Object[] { email, grantDomain }), result);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterDomainSendToDL()
/*     */     throws Exception
/*     */   {
/* 276 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 277 */     String user1email = TestUtil.getAddress("user1");
/* 278 */     Account user1account = TestUtil.getAccount("user1");
/* 279 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, user1account.getDomainName(), null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 282 */     doCheckSentToDistListDomRight(dl, user1email, user1account.getDomainName(), true);
/* 283 */     doCheckSentToDistListDomRight(dl, "pete@example.test", user1account.getDomainName(), false);
/* 284 */     doCheckSentToDistListDomRight(dl, user1email.toUpperCase(Locale.ENGLISH), user1account.getDomainName(), true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterDomainSendToDLWithAcctAliasSender()
/*     */     throws Exception
/*     */   {
/* 294 */     this.prov.createDomain("other.example.test", new HashMap());
/* 295 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 296 */     Account acct = this.prov.createAccount(this.auser, "test123", new HashMap());
/* 297 */     this.prov.addAlias(acct, this.alias);
/* 298 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, acct.getDomainName(), null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 301 */     doCheckSentToDistListDomRight(dl, this.alias, acct.getDomainName(), true);
/*     */   }
/*     */   
/*     */ 
/*     */   public void testMilterDomainSendToDLWithDlSender()
/*     */     throws Exception
/*     */   {
/* 308 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 309 */     DistributionList dl2 = this.prov.createDistributionList(this.listAddress2, new HashMap());
/* 310 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, dl2.getDomainName(), null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 313 */     doCheckSentToDistListDomRight(dl, this.listAddress2, dl2.getDomainName(), true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterDomainSendToDLWithDlAliasSender()
/*     */     throws Exception
/*     */   {
/* 322 */     this.prov.createDomain("other.example.test", new HashMap());
/* 323 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 324 */     DistributionList dl2 = this.prov.createDistributionList(this.listAddress2, new HashMap());
/* 325 */     this.prov.addAlias(dl2, this.dlalias);
/* 326 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, dl2.getDomainName(), null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 329 */     doCheckSentToDistListDomRight(dl, this.dlalias, dl2.getDomainName(), true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testMilterExternalDomainSendToDL()
/*     */     throws Exception
/*     */   {
/* 337 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 338 */     String user1email = TestUtil.getAddress("user1");
/* 339 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EXT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, "example.test", null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 342 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { dl.getName(), dl.getId() });
/* 343 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/* 344 */     assertNotNull("Unable to find Group object for DL by name", group);
/* 345 */     assertTrue("pete@example.test should be able to send to DL (in domain example.test)", this.accessMgr.canDo("pete@example.test", group, Rights.User.R_sendToDistList, false));
/*     */     
/* 347 */     assertFalse(String.format("%s should NOT be able to send to DL (in domain example.test)", new Object[] { user1email }), this.accessMgr.canDo(user1email, group, Rights.User.R_sendToDistList, false));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testMilterEdomWithLocalDomain()
/*     */     throws Exception
/*     */   {
/* 358 */     DistributionList dl = this.prov.createDistributionList(this.listAddress, new HashMap());
/* 359 */     String user1email = TestUtil.getAddress("user1");
/* 360 */     Account user1account = TestUtil.getAccount("user1");
/* 361 */     this.prov.grantRight("dl", TargetBy.name, this.listAddress, GranteeType.GT_EXT_DOMAIN.getCode(), GranteeSelector.GranteeBy.name, user1account.getDomainName(), null, "sendToDistList", (RightModifier)null);
/*     */     
/*     */ 
/* 364 */     ZimbraLog.test.info("DL name %s ID %s", new Object[] { dl.getName(), dl.getId() });
/* 365 */     Group group = this.prov.getGroupBasic(Key.DistributionListBy.name, this.listAddress);
/* 366 */     assertNotNull("Unable to find Group object for DL by name", group);
/* 367 */     assertTrue(String.format("%s should be able to send to DL (in domain %s)", new Object[] { user1email, user1account.getDomainName() }), this.accessMgr.canDo(user1email, group, Rights.User.R_sendToDistList, false));
/*     */     
/*     */ 
/* 370 */     String badName = "unconfigured@" + user1account.getDomainName();
/* 371 */     assertTrue(String.format("%s should be able to send to DL (in domain %s)", new Object[] { badName, user1account.getDomainName() }), this.accessMgr.canDo(badName, group, Rights.User.R_sendToDistList, false));
/*     */   }
/*     */   
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws ServiceException
/*     */   {
/* 378 */     DbPool.startup();
/* 379 */     MemcachedConnector.startup();
/*     */     
/* 381 */     CliUtil.toolSetup();
/* 382 */     TestUtil.runTest(TestDistListACL.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestDistListACL.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */